package cn.fzm.ssm.dao;

import cn.fzm.ssm.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Dremeue
 * @create 2019-10-12 13:45
 **/
public interface IRoleDao {

    @Select("select * from role where id in (select roleId from users_role where userId = #{userId})")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property ="roleName"),
            @Result(column = "roleDesc", property ="roleDesc"),
            @Result(column = "id", property = "permissions", many = @Many(select = "cn.fzm.ssm.dao.IPermissionDao.findByRoleId"))
    })
    public List<Role> findByUserId(String userId);

    @Select("select * from role")
    public List<Role> findAll();

    @Insert("insert into role(id, roleName, roleDesc) values(#{id}, #{roleName}, #{roleDesc})")
    public void save(Role role);

    @Select("select * from role where id not in (select roleId from users_role where userId = #{userId})")
    public List<Role> findOtherRole(String userId);

    @Select("select * from role where id = #{id}")
    public Role findById(String id);

    @Insert("insert into role_permission(permissionId, roleId) values(#{permissionId}, #{roleId})")
    public void addPermissionToRole(@Param("permissionId") String permissionId, @Param("roleId") String roleId);
}
